package com.liuche.service.erp.system;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import javax.annotation.Resource;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.liuche.common.date.DateUtil;
import com.liuche.dao.erpauto.SysUserMapper;
import com.liuche.dao.erpself.ISysUserDao;
import com.liuche.domain.dto.system.UserInfoDto;
import com.liuche.domain.erpentity.SysUser;
import com.liuche.domain.erplogentity.SysUserLog;
import com.liuche.service.erplog.SysUserLogService;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

@Service
public class HelloWorldService {
	@Autowired
     private SysUserMapper sysUserMapper;
	@Autowired
    private ISysUserDao iSysUserDao;
	@Autowired
	private SysUserLogService sysUserLogService;
	@Transactional
	public void AddUser(String account, String password, String realname, String job, String islogin, String[] roles,
			String depart, String user_join_day, String user_mobile, String user_birthplace, String user_enname,
			String user_sex, String loginname) throws Exception{
		SysUser _sysUser=new SysUser();
		_sysUser.setLoginname(account);
		_sysUser.setPassword(password);
		_sysUser.setRealname(realname);
		_sysUser.setJob(job);
		_sysUser.setEnablelogin(islogin == "1" ? true : false);
		_sysUser.setEnable(true);
		_sysUser.setBirthaddress(user_birthplace);
		_sysUser.setCellphone(user_mobile);
		_sysUser.setEmployeedate(DateUtil.StringToDate(user_join_day));
		_sysUser.setEnglishname(user_enname);
		_sysUser.setSex(Integer.valueOf(user_sex));
		_sysUser.setCreateby(loginname);
		_sysUser.setCreatetime(new Date());
		_sysUser.setLastlogintime(null);
		sysUserMapper.insert(_sysUser);
		sysUserLogService.writeLog("账号:"+loginname, _sysUser.getUserid().toString(), null);
		
		//int y=1/0;
	}
	@Transactional
	public void AddUserBatch(String account, String password, String realname, String job, String islogin, String[] roles,
			String depart, String user_join_day, String user_mobile, String user_birthplace, String user_enname,
			String user_sex, String loginname) throws Exception{
		List<SysUser> list=new ArrayList<SysUser>();
		for(int i=0;i<=5;i++){
			SysUser _sysUser=new SysUser();
			_sysUser.setLoginname(account);
			_sysUser.setPassword(password);
			_sysUser.setRealname(realname);
			_sysUser.setJob(job);
			_sysUser.setEnablelogin(islogin == "1" ? true : false);
			_sysUser.setEnable(true);
			_sysUser.setBirthaddress(user_birthplace);
			_sysUser.setCellphone(user_mobile);
			_sysUser.setEmployeedate(DateUtil.StringToDate(user_join_day));
			_sysUser.setEnglishname(user_enname);
			_sysUser.setSex(Integer.valueOf(user_sex));
			_sysUser.setCreateby(loginname);
			_sysUser.setCreatetime(new Date());
			_sysUser.setLastlogintime(null);
			list.add(_sysUser);
		}
		iSysUserDao.insertBatch(list);
		
		//int y=1/0;
	}
	public Object[] getUserDepartmentList(String key,String departname,int pageIndex, int pageSize){
		Object[] ret2 = new Object[2];
		PageHelper.startPage(pageIndex, pageSize);
		 List<UserInfoDto> list=iSysUserDao.getUserDepartmentList(key, departname);
		 long count=((Page) list).getTotal();
		 ret2[0]=list;
		 ret2[1]=count;
		return ret2;
	}
}
